package com.lanhai.zhonglai.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.utils.Tools;


/**
 * 小车指令。如果没有协议上的变化 ，可不用修改
 * @author zhengwei
 *
 */
@Service
public class AgvEventImpl implements IAgvEvent {
	@Autowired
	IAgv iAgv;

	@Autowired
	ISendData sendTools;
	
	
	@Override
	public void toRoad(AgvBean agv, int road) {
		if(agv==null ) {
			return;
		}
		
		byte[] buf = new byte[8]; 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)agv.getAgvId();
		buf[2] = 1;
		byte[] roads=Tools.toLH(road);
		buf[3] = roads[0];
		buf[4] = roads[1];  //高字节

		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
	   buf[7] = s;
		 
		//处理重复指令
		String cmd=Tools.byte2HexStr(buf);  
		if(sameCmd(agv, cmd, 3)) {
			return;
		} 
		 
		String remark="";
		if(road==0) {
			remark="回到主路";
		}else {
			remark=String.format("行驶路线【%d】", road);
		} 
		
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}

	@Override
	public void stopOrstart(AgvBean agv, boolean isStop) {
		byte[] buf = new byte[8];
		 
		
		buf[0] = (byte) 0xA1;
		buf[1] = (byte) agv.getAgvId();;
		buf[2] = 3;
		
		String remark;
		if (isStop) {
			buf[6] = 1;
			remark="交管停止";
		} else {
			buf[6] = 2;
			remark="交管启动";
		}

		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 
		buf[7] = s;
	 
		
		//处理重复指令
//		String cmd=Tools.byte2HexStr(buf);  
//		if(sameCmd(agv, cmd, 8)) {
//			return;
//		}
		 
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}

	@Override
	public void transUp(AgvBean agv, boolean isReceive) {
		byte[] buf = new byte[8];
	 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		
		String remark;
		if(isReceive) {
			//上层传入
			buf[5]=3;
			remark="上层传入";
		}else {
			//上层传出
			buf[5]=2;
			remark="上层传出";
		}
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 buf[7] = s;
		 
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}

	@Override
	public void transDown(AgvBean agv, boolean isReceive) {
		byte[] buf = new byte[8];
	 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		
		String remark;
		if(isReceive) {
			//下层传入
			buf[5]=5;
			remark="下层传入";
		}else {
			//下层传出
			buf[5]=4;
			remark="下层传出";
		}
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
	    buf[7] = s;
	 
		
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}

	@Override
	public void transUpOutAndDownIn(AgvBean agv) {
		byte[] buf = new byte[8];
	 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		

		 buf[5]=6;
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 buf[7] = s;
	 
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),"上出下入", buf); 
	}

	@Override
	public void transUpInAndDownOut(AgvBean agv) {
		byte[] buf = new byte[8];
	 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte) agv.getAgvId();
		buf[2] = 2; 
		 buf[5]=7; 
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 buf[7] = s;
		 
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),"上入下出", buf); 
	}

	@Override
	public void finishTrans(AgvBean agv) {
		byte[] buf = new byte[8];
	 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		
		
		 buf[5]=1;   //1=完成传输退回二维码
		 
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 
			buf[7] = s;
		 
		String remark="完成对接";
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
		
	}
	
	@Override
	public void stopTrans(AgvBean agv) {
		byte[] buf = new byte[8];
		 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;  //传输标识
		
		
		 buf[5]=0;   //0=停止传输
		 
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 buf[7] = s;
		 
		//处理重复指令
		//String cmd = Tools.byte2HexStr(buf);
		//if (sameCmd(agv, cmd, 20)) {
		//	return;
		//}
		
		
		String remark="停止传输";
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}
	
	@Override
	public void transDownLeftOut(AgvBean agv) {
		byte[] buf = new byte[8];
		 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;  //传输标识
		
		
		 buf[5]=12;    // 0C下左传出
		 
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		 
			buf[7] = s;
		 
		String remark="下左传出";
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}

	@Override
	public void transDownRightOut(AgvBean agv) {
		byte[] buf = new byte[8];
		 
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;  //传输标识
		
		
		 buf[5]=14;     // 0E下右传出
		 
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
	 
			buf[7] = s;
		 
		String remark="下右传出";
		sendTools.sendAgvData(agv.getAgvId(), agv.getIp(), agv.getPort(),remark, buf); 
	}
	
	
 
	boolean sameCmd(AgvBean agv, String cmd, int maxcount) {
		// 处理重复指令
		if (agv.getLastCmd() != null && agv.getLastCmd().equals(cmd) && agv.getCmdtime() >= maxcount) {
			// 重复指令，顶多发两次
			return true;
		}
		if (agv.getLastCmd() != null && agv.getLastCmd().equals(cmd)) {
			agv.addTime();
		} else {
			agv.setCmdtime(0);
		}
		agv.setLastCmd(cmd);
		iAgv.saveCache(agv);
		return false;
	}

	

	
	
}
